import sys

from main_page import Ui_MainWindow
from PyQt5.Qt import *
from PyQt5 import QtCore, QtGui


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.main_page = Ui_MainWindow()
        self.main_page.setupUi(self)

        self.left_btn_name_list = []
        self.right_btn_name_list = []

        self.add_btn()
        self.init_btn()
        self.connect_btn()

    def add_btn(self):
        self.left_btn_name_list.append(self.main_page.btn01.objectName())
        self.left_btn_name_list.append(self.main_page.btn02.objectName())
        self.left_btn_name_list.append(self.main_page.btn03.objectName())
        self.left_btn_name_list.append(self.main_page.btn04.objectName())
        self.left_btn_name_list.append(self.main_page.btn05.objectName())
        self.left_btn_name_list.append(self.main_page.btn06.objectName())

        self.right_btn_name_list.append(self.main_page.btn07.objectName())
        self.right_btn_name_list.append(self.main_page.btn08.objectName())
        self.right_btn_name_list.append(self.main_page.btn09.objectName())
        self.right_btn_name_list.append(self.main_page.btn10.objectName())
        self.right_btn_name_list.append(self.main_page.btn11.objectName())
        self.right_btn_name_list.append(self.main_page.btn12.objectName())

    def init_btn(self):
        for btn_name in self.left_btn_name_list:
            btn = self.findChild(QWidget, btn_name)
            btn.setProperty("selected", False)

        for btn_name1 in self.right_btn_name_list:
            btn = self.findChild(QWidget, btn_name1)
            btn.setProperty("selected", False)

    def connect_btn(self):
        self.main_page.btn01.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn01))
        self.main_page.btn02.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn02))
        self.main_page.btn03.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn03))
        self.main_page.btn04.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn04))
        self.main_page.btn05.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn05))
        self.main_page.btn06.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn06))
        self.main_page.btn07.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn07))
        self.main_page.btn08.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn08))
        self.main_page.btn09.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn09))
        self.main_page.btn10.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn10))
        self.main_page.btn11.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn11))
        self.main_page.btn12.clicked.connect(lambda: self.on_btn_clicked(self.main_page.btn12))

    def on_btn_clicked(self, btn):
        selected = btn.property("selected")
        self.btn_change_to_unselect(btn)
        if selected is False:
            self.btn_selected(btn, True)
        else:
            self.btn_selected(btn, False)

    def btn_change_to_unselect(self, current_btn):
        for btn_name in self.left_btn_name_list:
            btn = self.findChild(QWidget, btn_name)
            if btn == current_btn:
                continue
            selected = btn.property("selected")
            if selected is True:
                self.btn_selected(btn, False)

        for btn_name in self.right_btn_name_list:
            btn = self.findChild(QWidget, btn_name)
            if btn == current_btn:
                continue

            selected = btn.property("selected")
            if selected is True:
                self.btn_selected(btn, False)

    def btn_selected(self, btn, is_selected):
        btn_name = btn.objectName()
        btn_geometry = btn.geometry()
        if is_selected is True:
            btn.setProperty("selected", True)
            btn.setMinimumSize(QtCore.QSize(80, 40))
            btn.setMaximumSize(QtCore.QSize(80, 40))
            font = QtGui.QFont()
            font.setFamily("Microsoft YaHei")
            font.setPointSize(14)
            btn.setFont(font)
            btn.setAutoFillBackground(False)
            if btn_name in self.left_btn_name_list:
                btn.setStyleSheet("border-image: url(:/模块详情/images/模块详情/设备标签_左边_选中.png);\n"
                                  "color: rgb(157,164,187,1);")
            else:
                btn.setStyleSheet("border-image: url(:/模块详情/images/模块详情/设备标签_右边_选中.png);\n"
                                  "color: rgb(157,164,187,1);")

                print(btn.objectName(), "original geo", btn_geometry)
                btn_geometry.setX(btn_geometry.x() - 10)
                btn.setGeometry(btn_geometry)
                print(btn.objectName(), "selected geo", btn_geometry)
        else:
            btn.setProperty("selected", False)
            btn.setMinimumSize(QtCore.QSize(70, 40))
            btn.setMaximumSize(QtCore.QSize(70, 40))
            font = QtGui.QFont()
            font.setFamily("Microsoft YaHei")
            font.setPointSize(14)
            btn.setFont(font)
            btn.setAutoFillBackground(False)
            if btn_name in self.left_btn_name_list:
                btn.setStyleSheet("border-image: url(:/模块详情/images/模块详情/设备标签_左边_未选中.png);\n"
                                  "color: rgb(157,164,187,1);")
            else:
                btn.setStyleSheet("border-image: url(:/模块详情/images/模块详情/设备标签_右边_未选中.png);\n"
                                  "color: rgb(157,164,187,1);")

                print(btn.objectName(), " original geo", btn_geometry)
                btn_geometry.setX(btn_geometry.x() + 10)

                btn.setGeometry(btn_geometry)
                print(btn.objectName(), "unselected geo", btn_geometry)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
